package com.myfum.wingplayer.commands
{
	import com.myfum.wingplayer.entity.ScheduleItemData;
	import com.myfum.wingplayer.entity.ScheduleLineData;
	import com.myfum.wingplayer.events.TaskEvent;
	import com.myfum.wingplayer.model.ProgramLocator;
	import com.myfum.wingplayer.utils.FileManager;
	
	import flash.filesystem.File;
	
	import org.robotlegs.mvcs.Command;
	
	public class CheckTaskCommand extends Command
	{
		[Inject]
		public var fileManager:FileManager;
		[Inject]
		public var locator:ProgramLocator;
		
		public function CheckTaskCommand()
		{
			super();
		}
		
		override public function execute():void{
			var date:Date = new Date();
			var f:File = fileManager.getScheduleByDay(date.day);
			
			var sd:ScheduleLineData = new ScheduleLineData(f);
			sd.serialize();
			
			if(scheduleChanged(sd)){
				eventDispatcher.dispatchEvent(new TaskEvent(TaskEvent.SCHEDULE_CHANGED));
			}
		}
		
		private function scheduleChanged(data:ScheduleLineData):Boolean{
			if(locator.day != data.day){
				return true;
			}
			if(locator.arrayCollection.length != data.arrayCollection.length){
				return true;
			}
			var r:Boolean = false;
			for(var i:int = 0;i<locator.arrayCollection.length;i++){
				var s1:ScheduleItemData = locator.arrayCollection.getItemAt(i) as ScheduleItemData;
				var s2:ScheduleItemData = data.arrayCollection.getItemAt(i) as ScheduleItemData;
				if(s1.startMinutes != s2.startMinutes || s1.programPath != s2.programPath){
					r = true;
					break;
				}
			}
			return r;
		}
	}
}











